<html>
<head><meta charset="utf-8"><title>Is it possible to get information from a Span directly? · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20it.20possible.20to.20get.20information.20from.20a.20Span.20directly.3F.html">Is it possible to get information from a Span directly?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="232892057"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20it%20possible%20to%20get%20information%20from%20a%20Span%20directly%3F/near/232892057" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> GuillaumeGomez <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20it.20possible.20to.20get.20information.20from.20a.20Span.20directly.3F.html#232892057">(Apr 02 2021 at 12:13)</a>:</h4>
<p>I'm currently investigating multiple ways to add more information into the source code generated by rustdoc. For example, it would be nice to be able to click on a type to jump to its definition. Mostly like what cargo-src is doing in short.</p>
<p>For the moment, rustdoc is using <code>rustc_lexer</code> to do so and therefore only provides tokens and their associated span. Does anyone know if I can get information from a span directly and get its associated "Item" using the <code>TyCtxt</code> or anything else along the line?</p>
<p>Thanks in advance!</p>



<a name="232898749"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20it%20possible%20to%20get%20information%20from%20a%20Span%20directly%3F/near/232898749" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20it.20possible.20to.20get.20information.20from.20a.20Span.20directly.3F.html#232898749">(Apr 02 2021 at 13:31)</a>:</h4>
<p>I don't think that is possible. You need a HirId to get at any info. So I think you would need to build a map from byteoffsets to information. You can build this map from the HIR and then once you have it you can query it with spans' start/end</p>



<a name="232899313"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20it%20possible%20to%20get%20information%20from%20a%20Span%20directly%3F/near/232899313" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> GuillaumeGomez <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20it.20possible.20to.20get.20information.20from.20a.20Span.20directly.3F.html#232899313">(Apr 02 2021 at 13:37)</a>:</h4>
<p>Thanks for the answer. I had something similar in mind in the case that it wasn't possible directly from span. Is there a preferred way to go through all items from one crate or should I use a visitor?</p>



<a name="232899880"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20it%20possible%20to%20get%20information%20from%20a%20Span%20directly%3F/near/232899880" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20it.20possible.20to.20get.20information.20from.20a.20Span.20directly.3F.html#232899880">(Apr 02 2021 at 13:42)</a>:</h4>
<p>This is what rust-analyzer's source-to-def infra does, might be worth looking at</p>



<a name="232900215"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20it%20possible%20to%20get%20information%20from%20a%20Span%20directly%3F/near/232900215" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> GuillaumeGomez <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20it.20possible.20to.20get.20information.20from.20a.20Span.20directly.3F.html#232900215">(Apr 02 2021 at 13:45)</a>:</h4>
<p>Good idea!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>